package com.sanjishu.enterlab.electricity.sale.service;

import com.sanjishu.enterlab.electricity.sale.controller.Response;
import com.sanjishu.enterlab.electricity.sale.domain.ContractData;
import com.sanjishu.enterlab.electricity.sale.domain.ContractInfo;
import com.sanjishu.enterlab.electricity.sale.mapper.ContractDataMapper;
import com.sanjishu.enterlab.electricity.sale.mapper.ContractInfoMapper;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class ContractDataService {

    @Autowired
    private ContractDataMapper contractDataMapper;
    @Autowired
    private ContractInfoMapper contractInfoMapper;

    public HashMap<String, ContractData> findByContractInfoId(Integer contractInfoId) {
        List<ContractData> contractDataList = contractDataMapper.findByContractInfoId(contractInfoId);
        HashMap<String, ContractData> contractDataHashMap = new HashMap<>();
        if (contractDataList != null && contractDataList.size()>0) {
            for(ContractData contractData : contractDataList) {
                contractDataHashMap.put(contractData.getType().toString(), contractData);
            }
        }
        return contractDataHashMap;
    }

    public Response updateContractData(Integer types, Integer contractId, String filePath) {
        ContractInfo contractInfo = contractInfoMapper.selectByPrimaryKey(contractId);
        if (contractInfo != null) {
            ContractData contractData = contractDataMapper.getByContractIdAndType(contractId, types);
            if (contractData != null) {
                contractData.setFileUrl(filePath);
                contractData.setUpdateAt(new Date());
                contractDataMapper.updateByPrimaryKey(contractData);
            } else {
                contractData = createContractData(contractId, types, filePath);
            }
            return Response.buildSuccess("上传成功。");
        } else {
            return Response.getError("请先选择售电套餐，套餐绑定成功后可上传资料。");
        }

    }

    public ContractData createContractData(Integer contractId, Integer types, String filePath) {
        ContractData contractData = new ContractData();
        contractData.setContractInfoId(contractId);
        contractData.setType(types);
        contractData.setStatus(ContractData.STATUS_NORMAL);
        contractData.setCreateAt(new Date());
        contractData.setFileUrl(filePath);
        contractDataMapper.insert(contractData);
        return contractData;
    }

    public void passData(Integer id, Integer contractId, Integer types) {
        updateDataStatus(id, contractId, types, ContractData.STATUS_VERIFIED);
    }

    public void refuseData(Integer id, Integer contractId, Integer types) {
        updateDataStatus(id, contractId, types, ContractData.STATUS_UNVERIFIED);
    }

    public void updateDataStatus(Integer id, Integer contractId, Integer types, Integer status) {
        ContractData contractData = null;
        if (id != null) {
            contractData = contractDataMapper.selectByPrimaryKey(id);
        } else {
            contractData = createContractData(contractId, types, null);
        }
        if (contractData != null) {
            contractData.setStatus(status);
            contractDataMapper.updateByPrimaryKey(contractData);
        }
    }


}
